package com.qf.shopping.dao.impl;

import com.qf.shopping.dao.IProductTypeDao;
import com.qf.shopping.entity.ProductType;
import com.qf.shopping.utils.Tools;

import java.util.List;

/**商品类型与数据库的交互
 * @author 三旬
 * @version 1.0  2022/9/15
 */

public class ProductTypeDaoImpl implements IProductTypeDao {

    @Override
    public Integer insert(ProductType p) {
        String sql = "insert into t_product_type (name,pid,icon) values(?,?,?)";
        return Tools.update(sql,p.getName(),p.getPid(),p.getIcon());
    }

    @Override
    public Integer deleteById(Integer id) {
        String sql = "delete from t_product_type where id = ?";
        return Tools.update(sql,id);
    }

    @Override
    public Integer updateById(ProductType p) {
        String sql = "update t_product_type set name = ?,pid = ?, icon = ? where id = ?";
        return Tools.update(sql,p.getName(),p.getPid(),p.getIcon(),p.getId());
    }

    @Override
    public ProductType selectOneById(Integer id) {
        String sql = "select * from t_product_type where id = ?";
        return Tools.selectOne(sql,ProductType.class,id);
    }

    @Override
    public List<ProductType> selectAll() {
        String sql = "select * from t_product_type";
        return Tools.selectList(sql,ProductType.class);
    }

    @Override
    public List<ProductType> selectPage(Integer index, Integer size) {
        String sql =  "select * from t_product_type limit ?,?";
        return Tools.selectList(sql,ProductType.class,index,size);
    }

    @Override
    public Integer counts() {
        return selectAll().size();
    }

    @Override
    public Integer delChecked(List<Integer> list) {
        StringBuilder sql = new StringBuilder("delete from t_product_type where id  in(");
        for (int i = 0; i < list.size(); i++) {
            if(i < list.size() -1){
                sql.append("?,");
            }else{
                sql.append("?");
            }
        }
        sql.append(")");
        return Tools.update(sql.toString(),list.toArray());
    }

    @Override
    public List<ProductType> getProdctTypeListByPid(Integer pid) {
        String sql = "select * from t_product_type where pid = ?";
        return Tools.selectList(sql,ProductType.class,-1);
    }

    @Override
    public List<ProductType> getParentChildren(Integer id) {
        String sql = "select * from t_product_type where pid = ?";
        return Tools.selectList(sql,ProductType.class,id);
    }
}
